package com.ys.controller;

import com.ys.pojo.Administrators;
import com.ys.pojo.As;
import com.ys.pojo.Asa;
import com.ys.service.AdministratorsService;
import com.ys.service.MenuService;
import com.ys.util.logs;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class LoginContorller {
    @Resource
    private AdministratorsService administratorsService;
    @Resource
    private MenuService menuService;
    //登录页面
    @GetMapping("/login.do")
    public String login(){
        return "login";
    }

    @RequestMapping("/indesx")
    public String indesx(){

        return "indesx";
    }

    @RequestMapping("/index")
    public String index(Model model){
        Subject subject = SecurityUtils.getSubject();
//        System.out.println(subject.isPermitted("user:add")+"--------------");
        Session session = subject.getSession();
        Administrators administrators = (Administrators)session.getAttribute("objs");
//              菜单
        List<As> list = new ArrayList<>();
        String pwds = administrators.getPassword();
        menuService.findByAdminAndPwd(administrators.getAccount(),pwds).forEach(x->{
            As as  = new As();
            as.setName(x.getGname());
            as.setIcons(x.getIcons());
            System.out.println(x.getGname());
            List<Asa> asas = new ArrayList<>();
            menuService.findByAdminAndPwds(administrators.getAccount(),pwds,x.getId()).forEach(a->{
                asas.add(new Asa(a.getGname(),a.getIcons(),a.getUrl(),a.getId()));
            });
            as.setAsa(asas);
            list.add(as);
        });
//        list.forEach(x->{
//            System.out.println(x.getIcons()+"==================================");
//            x.getAsa().forEach(y->{
//                System.out.println(y.getIcon()+"11111111111111111111111111111");
//            });
//        });
        model.addAttribute("menus",list);
        return "index";
    }

    @PostMapping(value = "/login.do", produces = "html/text;charset=utf-8")
    public String login(@RequestParam("user")String user, @RequestParam("pwd")String pwd,HttpSession session,Model model){
        if(user==null&&user==""&&pwd==null&&pwd==""){
            return "login";
        }
            UsernamePasswordToken token = new UsernamePasswordToken(user,pwd);
            Subject subject = SecurityUtils.getSubject();

            Administrators administrators = null;
            String loginErr = null;
            try {
                subject.login(token);
                administrators = (Administrators)session.getAttribute("objs");
            }catch (UnknownAccountException ex){
                loginErr="用户名或密码错误！";
            }catch (IncorrectCredentialsException ex){
                loginErr="用户名或密码错误！";
            }catch (Exception ex){
                loginErr="应用程序错误!请联系管理员！";
            }
            if(loginErr!=null){
                model.addAttribute("loginErr",loginErr);
                return "login";
            }
            if(Integer.parseInt(administrators.getState())==2){
                model.addAttribute("loginErr",administrators.getAnames()+"-"+"-"+administrators.getAccount()+"-该账号已被停用!");
                logs.logger.info("登录失败!");
                return "login";
            }

            logs.logger.info("登录成功");
            return "redirect:/index";
        }

}
